﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using SpreadsheetGear;

namespace MachiningHelper.ToolFile
{
    public class ColToolFile
    {
        public bool AllowHBore { get; set; }
        public bool AllowVBore { get; set; }
        public bool AllowRoute { get; set; }
        public bool AllowSaw { get; set; }
        public string NestFromLeft { get; set; }
        public string OffsetToCenterLine { get; set; }
        public string PathtoMachinePost { get; set; }
        public bool MachineMirrorsParts { get; set; }
        public string MachineOrigin { get; set; }
        public string MachineType { get; set; }
        public string DrillThenRoute { get; set; }
        public string OptimizeDrilling { get; set; }
        public string NumberOfDecimalPlaces { get; set; }
        public string ToolGroup { get; set; }
        public string MachineName { get; set; }
        public string Units { get; set; }
        public string FileExtension { get; set; }
        public double ThicknessLeftForFirstNestPass { get; set; }
        public string SpoilBoardPenetration { get; set; }
        public string PathtoMachineCode { get; set; }
        public string HorizontalBoreMachineType { get; set; }
        public string AllowDoweling { get; set; }
        public string BinaryConversionType { get; set; }
        public string BarcodeFormat { get; set; }
        public string MinPartLengthForSinglePass { get; set; }
        public string MinPartAreaForSinglePass { get; set; }
        public string MakeBorderPrograms { get; set; }
        public string UseLineNumbers { get; set; }
        public string LineNumberSequenceCharacter { get; set; }
        public string LineNumberStartNo { get; set; }
        public string LineNumberIncrement { get; set; }
        public string LineNumberSkipCharacter { get; set; }
        public string A33 { get; set; }
        public string Format { get; set; }
        public string UseShortFileNames { get; set; }
        public string MinimizeToolDrops { get; set; }
        public string CombinedFilesPath { get; set; }
        public string NestRampOffset { get; set; }
        public string NestRampLength { get; set; }
        public string NoRotateQty { get; set; }
        public string NoRotateDia { get; set; }
        public string CenterLineNest { get; set; }
        public string TabHeight { get; set; }
        public string TabLength1 { get; set; }
        public string TabLength2 { get; set; }
        public string FilePrefix { get; set; }
        public string DebugMode { get; set; }
        public string UserDefinedSequence { get; set; }
        public string HMachineUnits { get; set; }
        public string DoHZones { get; set; }
        public string HGlueTime { get; set; }
        public string DoHZValue { get; set; }
        public string DontRouteSecondPass { get; set; }
        public string HMachineCodePath { get; set; }
        public string NestFromTop { get; set; }
        public string HRoute { get; set; }
        public string HFileExtension { get; set; }
        public string A58 { get; set; }
        public string A59 { get; set; }
        public string VDrillSpoilBoardPenetration { get; set; }
        public string Zreference { get; set; }
        public string Face5Face6Combine { get; set; }

        public static ColToolFile LoadToolFile(string toolFilePath)
        {
            var fi = new FileInfo(toolFilePath);

            if (!fi.Exists)
            {
                throw new Exception("未找到刀具文件:" + fi.FullName);
            }

            if (fi.Extension.ToUpper() != ".TLFX")
            {
                throw new Exception("请选择tlfx文件");
            }

            var toolfile = new ColToolFile();

            var book = Factory.GetWorkbook(toolFilePath);

            loadSettings(toolfile, book);

            return toolfile;
        }

        private static void loadSettings(ColToolFile toolfile, IWorkbook book)
        {
            var sheet = book.Worksheets["Settings"];
            var cells = sheet.UsedRange;

            toolfile.FileExtension = cells[16, 1].Text;
            toolfile.MachineName = cells[14, 1].Text;
            toolfile.MachineMirrorsParts = cells[7, 1].Text == "1" ? true : false;
            toolfile.ThicknessLeftForFirstNestPass = double.Parse(cells[17, 1].Text);
        }
    }
}
